<?php

class sfNotifications  

{
	
  public function msgUserNotification($touserId,$fromuserid,$title,$msgbody) 
	{
		
		  $user = UserPeer::retrieveByPk($touserId);

		//Check if user has turned off his email notifications

	      $UserMsg = new UserMsg();
	      $UserMsg->setFromUserId($fromuserid);		 
		  $UserMsg->setSubject($title);
	      $UserMsg->setToUserId($user->getId());
	 	  $UserMsg->setMsgText($msgbody);
	      $UserMsg->setFolderId('1');				
	      $UserMsg->setReadStatus('1');
		  $UserMsg->setType('regular');
	      $UserMsg->save();
	
		  $UserMsg->setThreadId($UserMsg->getId());
		  $UserMsg->save();
		
		
		
		
	}
	
  public function mailUser($userId,$title,$msgbody) 
  {

		  // determine customer from the request 'id' parameter
		  $user = UserPeer::retrieveByPk($userId);

		//Check if user has turned off his email notifications
		
		if ($user->getNotifications() == true) 
		{

		  $success = $this->sendMail($title, $msgbody, $user->getEmail());
		  /*
		  // class initialization
		  $mail = new sfMail();
		  $mail->initialize();
		  $mail->setMailer('smtp');
		  $mail->setHostname('smtp.gmail.com');
		  $mail->setPort('465');
		  $mail->setUsername('mail-bot@devunity.com');
		  $mail->setPassword('meta11ica01');
		  $mail->setCharset('utf-8');
		  $mail->setContentType('text/html');

		  // definition of the required parameters
		  $mail->setSender('donot-reply@devunity.com', 'Devunity Community');
		  $mail->setFrom('donot-reply@devunity.com', 'Devunity Community');
		  $mail->addReplyTo('donot-reply@devunity.com');

		  $mail->addAddress($user->getEmail());

		  $mail->setSubject($title);
		  $mail->setBody($msgbody);

		  // send the email
		  $mail->send();
		  */
		}
	  	  //return $this->renderText(true);

	}
	
	 public static function mailAnonUser($email,$title,$msgbody) 
	 {
		$success = sfNotifications::sendMail($title, $msgbody, $email);
		//return $this->renderText($success);
		  /*
		  // determine customer from the request 'id' parameter
		  //$user = UserPeer::retrieveByPk($userId);

		  // class initialization
		  $mail = new sfMail();
		  $mail->initialize();
		  $mail->setMailer('smtp');
		  $mail->setHostname('smtp.gmail.com');
		  $mail->setPort('465');
		  $mail->setUsername('mail-bot@devunity.com');
		  $mail->setPassword('meta11ica01');
		  $mail->setCharset('utf-8');
		  $mail->setContentType('text/html');

		  // definition of the required parameters
		  $mail->setSender('donot-reply@devunity.com', 'Devunity Community');
		  $mail->setFrom('donot-reply@devunity.com', 'Devunity Community');
		  $mail->addReplyTo('donot-reply@devunity.com');

		  $mail->addAddress($email);

		  $mail->setSubject($title);
		  $mail->setBody($msgbody);

		  // send the email
		  $mail->send();
	  	  //return $this->renderText(true);
	  	  */

	}
	
	private static function sendMail($subject, $body, $address)
	{
		//Load in the files we'll need
		require_once "swift-mail/Swift.php";
		require_once "swift-mail/Swift/Connection/SMTP.php";
		 
		//Start Swift
		$smtp = new Swift_Connection_SMTP("smtp.gmail.com", Swift_Connection_SMTP::PORT_SECURE, Swift_Connection_SMTP::ENC_TLS);
		$smtp->setUsername("mail-bot@devunity.com");
		$smtp->setpassword("meta11ica01");
		$swift = new Swift($smtp);

		//Create the message
		$message = new Swift_Message($subject, $body, "text/html");
		 
		//Now check if Swift actually sends it
		if ($swift->send($message, $address, "do-not-reply@devunity.com"))
		{
			return true;
		} 
		else 
		{
			return false;
		}
	}
	

  }